package com.itheima.test;

import com.itheima.domain.Items;
import com.itheima.domain.Orders;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class Demo1 {
    public static void main(String[] args) throws Exception{
        InputStream is = Resources.getResourceAsStream("sql.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = build.openSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> list = mapper.findAll();
        for (User user : list) {
            System.out.println("用户名:"+ user.getUsername());
            //从用户中获取订单信息
            List<Orders>  orders = user.getOrdersList();
            for (Orders o : orders) {
                System.out.println("订单编号:"+o.getNumber());
                //获取商品
                List<Items> itemsList = o.getItemsList();
                for (Items i : itemsList) {
                    System.out.println("商品名称:"+i.getName());
                }
            }
        }
        session.commit();
        session.close();
        is.close();

    }

}
